package com.zhkj.txg.module.mine.ui

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.vm.BalanceViewModel
import kotlinx.android.synthetic.main.activity_bind_ali.*

class BindAliPayActivity : ViewModelActivity<BalanceViewModel>() {

    companion object {
        fun bind(activity: Activity, requestCode: Int) {
            val intent = Intent(activity, BindAliPayActivity::class.java)
            activity.startActivityForResult(intent, requestCode)
        }
    }

    override fun setViewModel(): BalanceViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleBind
    override val layoutResId: Int = R.layout.activity_bind_ali

    override fun initUI() {
        btnBindConfirm.setOnClickListener {
            if (etBindName.text.isNullOrEmpty()) {
                toast(getString(R.string.bind_name_input))
                return@setOnClickListener
            }
            if (etBindAccount.text.isNullOrEmpty()) {
                toast(getString(R.string.bind_account_input))
                return@setOnClickListener
            }
            viewModel.bindAliPay(etBindName.text.toString(), etBindAccount.text.toString())
        }
    }

    override fun initViewModel() {
        // 绑定支付宝数据观察者
        viewModel.bindAliPayLiveData.observe(this, Observer {
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS) {
                toast(it.response?.msg ?: getString(R.string.bind_success))
                intent.putExtra("data", etBindAccount.text?.toString())
                setResult(RESULT_OK, intent)
                finish()
            }
        })
    }
}